package com.eddie.juc.base;

import com.eddie.juc.IThreadPool;

import java.util.concurrent.TimeUnit;

/**
 * @author Eddie
 */
public class DaemonDemo {
    private static final IThreadPool instance = IThreadPool.getInstance();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t开始运行，"
                    //没有设置daemon为true时默认为false，所以这里打印用户线程，否则为守护线程
                    + (Thread.currentThread().isDaemon() ? "守护线程" : "用户线程"));
            //如果为用户线程，各个线程之间相互独立，main线程结束后t1线程仍然存在
            //如果为守护线程，main主线程结束后t1也会停止->jvm虚拟机关闭
            while (true) {

            }
        }, "t1");
        //设置为守护线程
        t1.setDaemon(true);
        t1.start();
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(Thread.currentThread().getName() + "\t----end 主线程");
    }
}
